﻿using AutoMapper;
using Harmontronics.DataAccess.IOperate.Business;
using Harmontronics.DataAccess.Operate.Business;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Model;
using Harmontronics.Model.Entitys;
using Harmontronics.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource;
using static System.Collections.Specialized.BitVector32;

namespace Harmontronics.Service
{
    public class ProcessService : IProcessService
    {
        private readonly IStationOperate _stationOperate;
        private readonly IProcessOperate _processOperate;
        private readonly IMapper _mapper;
        private readonly IUnitOfWork _unitOfWork;
        public ProcessService(IUnitOfWork unitOfWork, IMapper mapper, IStationOperate stationOperate, IProcessOperate processOperate)
        {
            _mapper = mapper;
            _stationOperate = stationOperate;
            _processOperate = processOperate;
            _unitOfWork = unitOfWork;
        }
        public List<ProcessModel> MendingRange(List<Process> entities)
        {
            List<ProcessModel> models = new List<ProcessModel>();
            foreach (var entity in entities)
            {
                models.Add(Mending(entity));
            }
            return models;
        }
        public ProcessModel Mending(Process processEntity)
        {
            ProcessModel model = _mapper.Map<ProcessModel>(processEntity);
            Station entity = _stationOperate.GetStationById(processEntity.WorkStationId);
            if (entity != null)
            {
                model.WorkStation = entity;
            }
            else
            {
                model.WorkStation = new Station()
                {
                    WorkStationId = processEntity.WorkStationId
                };
            }
            return model;
        }
        public bool AddProcess(Process process)
        {
            try
            {
                _processOperate.AddProcess(process);
                _unitOfWork.Commit();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }


        public bool DeleteProcess(string id)
        {
            try
            {
                Process entity = _processOperate.GetProcessById(id);
                if (entity != null)
                {
                    _processOperate.DeleteProcess(entity);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {

                return false;
            }
        }

        public List<Process> GetAllProcess()
        {
            try
            {
                return _processOperate.GetAllProcessList();
            }
            catch (Exception ex)
            {
                return new List<Process>();
            }
        }
        public List<ProcessModel> GetAllProcessModels()
        {
            try
            {
                List<ProcessModel> models = new List<ProcessModel>();
                List<Process> entities = _processOperate.GetAllProcessList();
                foreach (var entity in entities)
                {
                    models.Add(Mending(entity));
                }
                return models;
            }
            catch (Exception ex)
            {
                return new List<ProcessModel>();
            }
        }

        public ProcessModel GetProcessById(string id)
        {
            try
            {
                Process entity = _processOperate.GetProcessById(id);
                if (entity != null)
                {
                    return Mending(entity);
                }
            }
            catch (Exception e)
            {
            }
            return null;
        }


        public bool UpdateProcess(Process model)
        {
            try
            {
                Process entity = _processOperate.GetProcessById(model.ProcessId);
                if (entity != null)
                {
                    _mapper.Map(model, entity);
                    _processOperate.UpdateProcess(entity);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public bool BatchDelProcess(List<string> ids)
        {
            try
            {
                List<Process> entities = _processOperate.GetProcessByIdList(ids);
                if (entities.Count > 0)
                {
                    _processOperate.BatchDelProcess(entities);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }
    }
}
